const fs = require("fs");
const fse = require('fs-extra');
const plist = require('plist');
const images = require("images");
const walkSync = require('walk-sync');

//图集分解 - 需保证plist和png路径一致。

let sourcePath = process.argv[2];
if (sourcePath) {
    //获取plist文件名称
    let plistArray = walkSync(sourcePath, { globs: ['**/*.plist'] });
    //读取plist文件内容
    for (let i = 0, j = plistArray.length; i < j; i++) {
        let plistFile = fs.readFileSync(sourcePath + '\\' + plistArray[i], 'utf8');
        let plistData = plist.parse(plistFile) //用plist转内容为Js对象<解析XML>

        //根据plist名号生成文件夹 - 存放分解的图片
        let dirName = plistArray[i].split('.')[0];
        fse.ensureDirSync('./Image/' + dirName);
        // console.log('++=',  dirName)

        let big_Image = images(sourcePath + '\\' + dirName + '.png');
        for (let key in plistData.frames) {
            //获取plist中图片x,y ,width, height
            let rectStr = plistData.frames[key].textureRect.match(/\d+(.\d+)?/g);
            let rectArr = rectStr.join(',').split(',');
            //根据plist数据判断是否旋转
            if (plistData.frames[key].textureRotated) {
                //rotate(270)虽然摆正图片，但存在错位阴影问题。所以这里不进行旋转。
                images(big_Image, Number(rectArr[0]), Number(rectArr[1]), Number(rectArr[3]), Number(rectArr[2])).rotate(0).save(`./Image/${dirName}/${key}`);
            }
            else {
                images(big_Image, Number(rectArr[0]), Number(rectArr[1]), Number(rectArr[2]), Number(rectArr[3])).save(`./Image/${dirName}/${key}`);
            }
        }
    }
}


//图集合成 - 需保证plist和png和散图文件路径一致，且散图名称和图集名称一样。
// if (sourcePath) {
//     //获取plist文件名称
//     let plistArray = walkSync(sourcePath, { globs: ['**/*.plist'] });
//     //读取plist文件内容
//     for (let i = 0, j = plistArray.length; i < j; i++) {
//         let plistFile = fs.readFileSync(sourcePath + '\\' + plistArray[i], 'utf8');
//         let plistData = plist.parse(plistFile) //用plist转内容为Js对象<解析XML>

//         //根据plist名号生成文件夹 - 存放合成的图片
//         let dirName = plistArray[i].split('.')[0];
//         fse.ensureDirSync('./Atlas/' + dirName);

//         //根据plist中获取尺寸,创建透明图片
//         let sizeStr = plistData.metadata.size.match(/\d+(.\d+)?/g);
//         let sizeArr = sizeStr.join(',').split(',');
//         let big_Image = images(Number(sizeArr[0]), Number(sizeArr[1]));

//         for (let key in plistData.frames) {
//             //获取plist中图片x,y
//             let rectStr = plistData.frames[key].textureRect.match(/\d+(.\d+)?/g);
//             let rectArr = rectStr.join(',').split(',');
//             //根据plist数据判断是否旋转
//             if (plistData.frames[key].textureRotated) {
//                 big_Image.draw(images(sourcePath + '/' + dirName + '/' + key).rotate(90), Number(rectArr[0]), Number(rectArr[1]))
//             }
//             else {
//                 big_Image.draw(images(sourcePath + '/' + dirName + '/' + key), Number(rectArr[0]), Number(rectArr[1]))
//             }
//         }

//         big_Image.save(`Atlas/${dirName}/${dirName}.png`);
//     }
// }
